﻿using ATM.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ATM.Controllers;

namespace ATM.Controllers
{
    public class LogController : Controller
    {
        //ATMEntities _atm = new ATMEntities();
        ATM.Models.ATMEntities _atm = null;
        public LogController()
        {
            _atm = new Models.ATMEntities();
        }

        //
        // GET: /Log/

        public ActionResult SelectTime()
        {
            return View();
        }

        public ActionResult ListLog(int? day, int page = 1)
        {
            //Page size
            int pageSize = 10;
            try
            {
                DateTime _date;
                if (day == null)
                {
                    return returnViewSelection();
                }
                else
                {
                    ViewData[StaticValue.KEY_DAY] = day;
                    _date = DateTime.Now - TimeSpan.FromDays(double.Parse(day.ToString()));
                }
                String abc = System.Web.HttpContext.Current.Session["card"].ToString();
                List<Log> Logs = _atm.Logs.Where(b => b.CardNo.Equals(abc) && b.LogDate >= _date).Select(b => b).ToList();
                //paging 
                PagedList.PagedList<Log> pagelistLogs = new PagedList.PagedList<Log>(Logs, page, pageSize);
                
                ViewData[StaticValue.KEY_LIST_LOGS] = pagelistLogs;

                return returnViewListLog();
            }
            catch (Exception)
            {
                return returnViewSelection();
            }

        }

        private ViewResult returnViewSelection()
        {
            return View("SelectTime");
        }

        private ViewResult returnViewListLog()
        {
            return View("ListLog");
        }

    }
}
